/*
 * HoangTN
 * servlet controller for pay01010.jsp
 */
package vn.edu.fpt.xml.hrm.controller.payroll;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.w3c.dom.Document;
import vn.edu.fpt.xml.hrm.common.Marshall;
import vn.edu.fpt.xml.hrm.controller.AbstractController;
import vn.edu.fpt.xml.hrm.controller.timesheet.TimeSheetUtil;
import vn.edu.fpt.xml.hrm.model.PayrollBean;
import vn.edu.fpt.xml.hrm.pojo.Payroll;
import vn.edu.fpt.xml.hrm.pojo.Payrolls;

/**
 *
 * @author HoangTN
 */
public class SearchPayrollController extends AbstractController {

    private final String homePage = "jsp/pay01010.jsp";
    private final String errorPage = "jsp/error.jsp";

    @Override
    protected String execute() {

        HttpServletRequest request = this.getRequest();
        HttpServletResponse response = this.getResponse();

        String action = request.getParameter("action");
        String url = errorPage;
        if (action.equals("Search")) {
            try {
                String webPath = getServletContext().getRealPath("/");
                String filePath = webPath + "WEB-INF/payroll.xml";

                Document doc = TimeSheetUtil.createDOMTree(filePath);

                String empID = request.getSession().getAttribute("EMPLOYEEID").toString();
                String role = request.getSession().getAttribute("ROLE").toString();

                int fromMonth = Integer.parseInt(request.getParameter("fromMonth"));
                int fromYear = Integer.parseInt(request.getParameter("fromYear"));
                int toMonth = Integer.parseInt(request.getParameter("toMonth"));
                int toYear = Integer.parseInt(request.getParameter("toYear"));

                PayrollBean bean = new PayrollBean();
                List<Payroll> results = new ArrayList<Payroll>();
                if (role.equals("admin")) {
                    String empStatus = request.getParameter("cboEmpStatus");
                    String strAmount = request.getParameter("txtAmount");
                    int amount = Integer.parseInt(strAmount);
                    results = bean.getByMonthYearForAdmin(fromMonth,
                            fromYear, toMonth, toYear, empID, role, empStatus, amount);
                }
                if (role.equals("employee")) {
                    results = bean.getByMonthYear(fromMonth,
                            fromYear, toMonth, toYear, empID, role);
                }
                Payrolls list = new Payrolls();
                list.setPayroll(results);

                url = homePage;
                HttpSession session = request.getSession();

                request.setAttribute("checked", "checked");
                Marshall.marshall(filePath, list);

            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
        return url;
    }
}
//            try {
//                XPathFactory xpf = XPathFactory.newInstance();
//                XPath xpath = xpf.newXPath();
//                String exp = "//payroll[payrollDate >= " + fromDate + " and payrollDate <= " + toDate + "]";
//                NodeList result = (NodeList) xpath.evaluate(exp, doc, XPathConstants.NODESET);
//
////                List list = addToList(result);
////                if (!list.isEmpty()) {
////                    HttpSession session = request.getSession();
////                    session.setAttribute("result", list);
////                    url = homePage;
////                }
//
//            } catch (XPathExpressionException ex) {
//                Logger.getLogger(SearchPayrollController.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
//        return url;
//    }
//
//    private void addToList(NodeList nodeList) {
////        List list = null;
////        int a = 0,
////                b = 0,
////                c = 0,
////                d = 0;
////
////        for (int i = 0; i < nodeList.getLength(); i++) {
////            if (nodeList.item(i).getNodeName().equals("payroll")) {
////                Node payrollNode = nodeList.item(i);
////
////                Payroll payroll = new Payroll();
////                payroll.setPayrollID(payrollNode.getAttributes().getNamedItem("payrollID").
////                        getNodeValue());
////
////                NodeList payrollEls = payrollNode.getChildNodes();
////                for (int j = 0; j < payrollEls.getLength(); j++) {
////                    Node el = payrollEls.item(j);
////                    if (el.getNodeName().equals("employeeID")) {
////                        payroll.setEmployeeID(el.getTextContent().trim());
////                        a = 1;
////                    }
////                    if (el.getNodeName().equals("payrollDate")) {
////                        payroll.setPayrollDate(el.getTextContent().trim());
////                        b = 1;
////                    }
////                    if (el.getNodeName().equals("amount")) {
////                        payroll.setAmount(Float.parseFloat(el.getTextContent().trim()));
////                        c = 1;
////                    }
////                    if (el.getNodeName().equals("payrollType")) {
////                        payroll.setPayrollType(el.getTextContent().trim());
////                        d = 1;
////                    }
////                    if ((a * b * c * d) == 1) {
////                        list.add(payroll);
////                    }
////                }
////            }
////        }
////        return list;
//    }
//}

